import { defineStore } from "pinia";
import { $endApi } from "@/api/base/index";

export const useHomeStore = defineStore('home', {
  // 静态数据
  state: () => ({
    schoolName: '',
    pipelineLeakageData: {
      waterSupply: '0',
      waterConsumption: '0',
      leakageAmount: '0',
      leakageRate: '0',
    },
    waterConsumptionData: {
      today: '0',
      yesterday: '0',
      increaseRate: '±0%',
    },
    powerConsumptionData: {
      today: '0',
      yesterday: '0',
      increaseRate: '±0%',
    },
    gasConsumptionData: {
      today: '0',
      yesterday: '0',
      increaseRate: '±0%',
    },
    standardCoalQuantity: {
      today: '0',
      yesterday: '0',
      increaseRate: '±0%',
    },
    perCapitaEnergyConsumption: {
      water: {
        yestoday: '0',
        perCapitaPerDay: '0',
        lastMonthWater: '0',
        perCapitaPerMonth: '0',
      },
      electricity: {
        yestoday: '0',
        perCapitaPerDay: '0',
        lastMonthWater: '0',
        perCapitaPerMonth: '0',
      },
      standardCoal: {
        yestoday: '0',
        perCapitaPerDay: '0',
        lastMonthWater: '0',
        perCapitaPerMonth: '0',
      }
    },
    implementPressureData:{
      canteen: '0.0',
      mainTeachingBuilding: '0.0',
      library: '0.0',
    },
    implementPressureDataTitle:[
      '食堂',
      '主教学楼',
      '图书馆'
    ],
    implementPressureDataObj: [
      {
        title: '食堂',
        value: '0.0'
      },
      {
        title: '主教学楼',
        value: '0.0'
      },
      {
        title: '图书馆',
        value: '0.0'
      },
    ],
    alarmInfoData: [
      {title:'图书馆一楼水表1',value:'流量超标'},
      {title:'图书馆一楼水表2',value:'流量超标'},
      {title:'图书馆一楼水表3',value:'流量超标'},
      {title:'图书馆一楼水表4',value:'用电负荷超标'},
      {title:'图书馆一楼水表5',value:'用电负荷超标'},
      {title:'图书馆一楼水表6',value:'用电负荷超标'},
    ],
    equipmentOverviewData: [],
    waterSaveData: {
      date: [],
      base:  [],
      actual: [],
    },
    energyTrendData: {
      date: ['11-01', '11-02', '11-03', '11-04', '11-05', '11-06', '11-07'],
      water: [120, 132, 101, 134, 90, 230, 210],
      electricity: [2220, 2182, 2191, 2234, 2290, 2330, 2310],
      gas: [220, 182, 191, 234, 290, 330, 310],
      standardCoal: [1220, 1182, 1191, 1234, 2190, 1330, 1310],
    },
    quotaAssessmentData: []
  }),
  // 相当于计算属性(有数据缓存)
  getters: {},
  // actions即可以是同步函数也可以是异步函数
  actions: {
    // 获取管网漏损分析
    async setPipelineLeakageData() {
      try {
        const res = await $endApi.v1.home.energyDatabordLeakageAnalysis({});
        if(res?.data && res?.data.length > 0) {
          this.pipelineLeakageData.waterSupply = res?.data.find(item => item.title === 'waterSupply')?.value || '0';
          this.pipelineLeakageData.waterConsumption = res?.data.find(item => item.title === 'waterConsumption')?.value || '0';
          this.pipelineLeakageData.leakageAmount = res?.data.find(item => item.title === 'leakageAmount')?.value || '0';
          this.pipelineLeakageData.leakageRate = res?.data.find(item => item.title === 'leakageRate')?.value || '0';
        }
      } catch {}
    },
    // 获取用水量数据
    async setWaterConsumptionData() {
      try {
        const res = await $endApi.v1.home.energyDatabordWaterConsumption({});
        if(res?.data && res?.data.length > 0) {
          this.waterConsumptionData.today = res?.data.find(item => item.title === 'today')?.value || '0';
          this.waterConsumptionData.yesterday = res?.data.find(item => item.title === 'yesterday')?.value || '0';
          this.waterConsumptionData.increaseRate = res?.data.find(item => item.title === 'increaseRate')?.value || '0';
        }
      } catch {}
    },
    // 获取用电量
    async setEnergyDatabordPowerConsumption() {
      try {
        const res = await $endApi.v1.home.energyDatabordPowerConsumption({});
        if(res?.data && res?.data.length > 0) {
          this.powerConsumptionData.today = res?.data.find(item => item.title === 'today')?.value || '0';
          this.powerConsumptionData.yesterday = res?.data.find(item => item.title === 'yesterday')?.value || '0';
          this.powerConsumptionData.increaseRate = res?.data.find(item => item.title === 'increaseRate')?.value || '0';
        }
      } catch {}
    },
    // 获取用气量接口
    async setEnergyDatabordGasConsumption() {
      try {
        const res = await $endApi.v1.home.energyDatabordGasConsumption({});
        if(res?.data && res?.data.length > 0) {
          this.gasConsumptionData.today = res?.data.find(item => item.title === 'today')?.value || '0';
          this.gasConsumptionData.yesterday = res?.data.find(item => item.title === 'yesterday')?.value || '0';
          this.gasConsumptionData.increaseRate = res?.data.find(item => item.title === 'increaseRate')?.value || '0';
        }
      } catch {}
    },
    // 获取标煤量接口
    async setEnergyDatabordStandardCoal() {
      try {
        const res = await $endApi.v1.home.energyDatabordStandardCoal({});
        if(res?.data && res?.data.length > 0) {
          this.standardCoalQuantity.today = res?.data.find(item => item.title === 'today')?.value || '0';
          this.standardCoalQuantity.yesterday = res?.data.find(item => item.title === 'yesterday')?.value || '0';
          this.standardCoalQuantity.increaseRate = res?.data.find(item => item.title === 'increaseRate')?.value || '0';
        }
      } catch {}
    },
    // 获取人均用能接口
    async setEnergyDatabordAverageEnergy() {
      try {
        const res = await $endApi.v1.home.energyDatabordAverageEnergy({});
        if(res?.data) {
          const water = res.data['water'] || [];
          const electricity = res.data['electricity'] || [];
          const standardCoal = res.data['coal'] || [];
          this.perCapitaEnergyConsumption = {
            water: {
              yestoday: water.find(item => item.title === 'yestodayWater')?.value || '0',
              perCapitaPerDay: water.find(item => item.title === 'perCapitaPerDay')?.value || '0',
              lastMonthWater: water.find(item => item.title === 'lastMonthWater')?.value || '0',
              perCapitaPerMonth: water.find(item => item.title === 'perCapitaPerMonth')?.value || '0',
            },
            electricity: {
              yestoday: electricity.find(item => item.title === 'yestodayElectricity')?.value || '0',
              perCapitaPerDay: electricity.find(item => item.title === 'perCapitaPerDay')?.value || '0',
              lastMonthWater: electricity.find(item => item.title === 'lastMonthElectricity')?.value || '0',
              perCapitaPerMonth: electricity.find(item => item.title === 'perCapitaPerMonth')?.value || '0',
            },
            standardCoal: {
              yestoday: standardCoal.find(item => item.title === 'yestodayCoal')?.value || '0',
              perCapitaPerDay: standardCoal.find(item => item.title === 'perCapitaPerDay')?.value || '0',
              lastMonthWater: standardCoal.find(item => item.title === 'lastMonthCoal')?.value || '0',
              perCapitaPerMonth: standardCoal.find(item => item.title === 'perCapitaPerMonth')?.value || '0',
            }
          }
        }
      } catch {}
    },

     // 获取实时压力接口
     async setEnergyDatabordRealTimePressure() {
      try {
        const res = await $endApi.v1.home.energyDatabordRealTimePressure({});
        if(res?.data) {
          this.implementPressureDataObj = res?.data;
          // this.implementPressureDataObj.push({
          //   title: '1231',
          //   value: '0.5'
          // });
          // this.implementPressureDataObj.push({
          //   title: '12314',
          //   value: '0.5'
          // });
          // this.implementPressureDataObj.push({
          //   title: '12315',
          //   value: '0.5'
          // });
          // const val1 = res?.data[0]?.value;
          // const val2 = res?.data[1]?.value;
          // const val3 = res?.data[2]?.value;
          // this.implementPressureDataTitle['0'] = res?.data[0]?.title;
          // this.implementPressureDataTitle['1'] = res?.data[1]?.title;
          // this.implementPressureDataTitle['2'] = res?.data[2]?.title;
          // this.implementPressureData.canteen = val1 ? (val1 > 1 ? '0.0' : val1) : '0.0';
          // this.implementPressureData.mainTeachingBuilding =  val2 ? (val2 > 1 ? '0.0' : val2) : '0.0';
          // this.implementPressureData.library =  val3 ? (val3 > 1 ? '0.0' : val3) : '0.0';
        }
      } catch {}
    },
    // 获取定额考核接口
    async setEnergyDatabordQuotaAssessment() {
      try {
        const res = await $endApi.v1.home.energyDatabordQuotaAssessment({});
        if(res?.data) {
          this.quotaAssessmentData = res?.data;
        }
      } catch {}
    },
    // 获取定额考核接口
    async setEnergyDatabordEquipmentOverview() {
      try {
        const res = await $endApi.v1.home.energyDatabordEquipmentOverview({});
        if(res?.data) {
          this.equipmentOverviewData = res?.data;
        }
      } catch {}
    },
    
    // 获取告警信息接口
    async setEnergyDatabordAlarmInfo() {
      try {
        const res = await $endApi.v1.home.energyDatabordAlarmInfo({});
        if(res?.data) {
          this.alarmInfoData = res?.data;
        }
      } catch {}
    },
    // 获取节水数据接口
    async setEnergyDatabordWaterSave() {
      try {
        const res = await $endApi.v1.home.energyDatabordWaterSave({});
        if(res?.data) {
          this.waterSaveData = {
            date: res?.data?.actualWaterVolume?.date || 
            ["11-01", "11-02", "11-03", "11-04", "11-05", "11-06", "11-07"],
            base: res?.data?.basicWaterVolume?.value.map((item=> Number(item))) || [0, 0, 0, 0, 0, 0, 0],
            actual: res?.data?.actualWaterVolume?.value.map((item=> Number(item))) || [0, 0, 0, 0, 0, 0, 0],
          };
        }
      } catch {}
    },
    // 获取节水数据接口
    async setEnergyDatabordEnergyTrend() {
      try {
        const res = await $endApi.v1.home.energyDatabordEnergyTrend({});
        if(res?.data) {
          this.energyTrendData = {
            date: res?.data?.date || 
            ["11-01", "11-02", "11-03", "11-04", "11-05", "11-06", "11-07"],
            water: res?.data?.water?.map(item=>Number(item)) || [120, 132, 101, 134, 90, 230, 210],
            electricity: res?.data?.electricity?.map(item=>Number(item)) || [2220, 2182, 2191, 2234, 2290, 2330, 2310],
            gas: res?.data?.gas?.map(item=>Number(item)) || [220, 182, 191, 234, 290, 330, 310],
            standardCoal: res?.data?.standardCoal?.map(item=>Number(item)) || [1220, 1182, 1191, 1234, 2190, 1330, 1310],
          };
        }
      } catch {}
    },
    // 获取学校名称
    async getScoolName() {
      try {
        const res = await $endApi.v1.home.energyDatabordCollegeName({});
        if(res?.data) {
          this.schoolName = res?.data?.value || '';
        }
      } catch {}
    },
    // 更新所有数据
    async initData() {
      this.getScoolName();
      this.setPipelineLeakageData();
      this.setWaterConsumptionData();
      this.setEnergyDatabordPowerConsumption();
      this.setEnergyDatabordGasConsumption();
      this.setEnergyDatabordStandardCoal();
      this.setEnergyDatabordAverageEnergy();
      this.setEnergyDatabordRealTimePressure();
      this.setEnergyDatabordQuotaAssessment();
      this.setEnergyDatabordEquipmentOverview();
      this.setEnergyDatabordAlarmInfo();
      this.setEnergyDatabordWaterSave();
      this.setEnergyDatabordEnergyTrend();
    },

  },
  // 开启数据缓存
  // persist: {
  //   enabled: true,
  //   strategies: [
  //     {
  //       storage: sessionStorage,
  //     },
  //   ],
  // },
});
